//
//  YYECommentsViewController.m
//  YuYingEDU
//
//  Created by 胡波 on 2018/1/17.
//  Copyright © 2018年 胡波. All rights reserved.
//

#import "YYECommentsViewController.h"
#import "UITextView+Placeholder.h"

@interface YYECommentsViewController ()

@property (nonatomic,strong) UITextView *textView;
@property (nonatomic,strong) NSMutableArray *buttonArr;
@property (nonatomic,assign) NSInteger score;
@property (nonatomic,strong) UILabel *scoreLabel;

@end

@implementation YYECommentsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = GeneralBackColor;
    
    self.score = 0;
    
    self.navigationItem.title = @"评论";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"发布" style:UIBarButtonItemStyleDone target:self action:@selector(sendClicked)];
    [self.navigationItem.rightBarButtonItem setTintColor:[UIColor blackColor]];
    
    [self configUI];
    
}

- (void)configUI {
    
    UILabel *commentsLabel = [[UILabel alloc]init];
    commentsLabel.text = @"评论";
    commentsLabel.textColor = [UIColor blackColor];
    commentsLabel.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:commentsLabel];
    
    [commentsLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.left).offset(19);
        make.top.equalTo(self.view.top).offset(22);
    }];
    
    for (NSInteger i = 0; i < 5; i++) {
        
        UIButton *commentBtn = [[UIButton alloc]init];
        [commentBtn setImage:[UIImage imageNamed:@"score"] forState:UIControlStateNormal];
        [commentBtn setImage:[UIImage imageNamed:@"score_sel"] forState:UIControlStateSelected];
        commentBtn.frame = CGRectMake(65 + i *35, 19, 25, 25);
        commentBtn.tag = 101 + i;
        [commentBtn addTarget:self action:@selector(scoreClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self.buttonArr addObject:commentBtn];
        [self.view addSubview:commentBtn];
        
    }
    
    self.scoreLabel = [[UILabel alloc]init];
    self.scoreLabel.textColor = GeneralGrayColor;
    self.scoreLabel.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:self.scoreLabel];
    
    UIView *contentView = [[UIView alloc]init];
    contentView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:contentView];
    
    self.textView = [[UITextView alloc]init];
    self.textView.font = [UIFont systemFontOfSize:15];
    self.textView.placeholder = @"请输入评论";
    self.textView.placeholderLabel.font = [UIFont systemFontOfSize:15];
    self.textView.placeholderLabel.textColor = LineColor;
    [contentView addSubview:self.textView];
    
    [self.scoreLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.left).offset(250);
        make.top.equalTo(self.view.center).offset(19);
        make.height.equalTo(25);
    }];
    
    [contentView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.left).offset(8);
        make.right.equalTo(self.view.right).offset(-8);
        make.top.equalTo(commentsLabel.bottom).offset(22);
        make.height.equalTo(SCREEN_WIDTH * 0.65);
    }];
    
    [self.textView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(UIEdgeInsetsMake(11, 11, 11, 11));
    }];
    
}

- (NSMutableArray *)buttonArr {
    
    if (!_buttonArr) {
        _buttonArr = [NSMutableArray array];
    }
    return _buttonArr;
    
}

// 评分
- (void)scoreClicked:(UIButton *)button {
    
    self.score = (button.tag - 100) * 2;
    self.scoreLabel.text = [NSString stringWithFormat:@"%.1f",@(button.tag - 100).floatValue * 2];
    
    for (NSInteger i = 0; i < button.tag - 100; i++) {
        
        UIButton *selectedBtn = self.buttonArr[i];
        selectedBtn.selected = YES;
        
    }
    
    for (NSInteger i = 4; i > button.tag - 101; i--) {
        
        UIButton *noselectBtn = self.buttonArr[i];
        noselectBtn.selected = NO;
        
    }

}

// 发布
- (void)sendClicked {
    
    if (self.score < 1) {
        
        [SVProgressHUD showWithStatus:@"请先评分"];
        [SVProgressHUD dismissWithDelay:0.5];
        
    }else if (self.textView.text.length < 1) {
        
        [SVProgressHUD showWithStatus:@"请填写评论内容"];
        [SVProgressHUD dismissWithDelay:0.5];
        
    }else {
        
        NSDictionary *dic = @{
                              @"uid":[LoginModel shareLogin].uid,
                              @"username":[LoginModel shareLogin].username,
                              @"token":[LoginModel shareLogin].token,
                              @"cid":self.cid,
                              @"score":@(self.score),
                              @"comment":self.textView.text
                              };
        [SVProgressHUD show];
        [AFNHelper post:AddComments parameters:dic success:^(id responseObject) {
            
            NSLog(@"%@",responseObject);
            if ([responseObject[@"status"] isEqual:@1]) {
                [self.navigationController popViewControllerAnimated:YES];
            }
            [SVProgressHUD showWithStatus:responseObject[@"message"]];
            [SVProgressHUD dismiss];
            
        } faliure:^(id error) {
            
            NSLog(@"请求失败");
            [SVProgressHUD showWithStatus:ShowError];
            [SVProgressHUD dismissWithDelay:0.5];
            
        }];
        
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
